package com.example.jyapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jyapplication.R;
import com.example.jyapplication.bean.UserBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnLogin;
    private EditText etName,etPassword;
    private TextView tvRegist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }
    private void initView() {
        // 绑定控件
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        tvRegist = findViewById(R.id.tv_regist);
        tvRegist.setOnClickListener(this);

        etName = findViewById(R.id.et_login_name);
        etPassword = findViewById(R.id.et_login_password);
    }

    @Override
    public void onClick(View v) {
        // 设置按钮的点击事件
        String name = etName.getText().toString();
        String password = etPassword.getText().toString();
        switch (v.getId()){
            //登录按钮点击事件
            case R.id.btn_login:
                //判空处理
                if (!name.equals("")&&!password.equals("")){
                        login();
                }else {
                    Toast.makeText(this, "用户名和密码不能为空！", Toast.LENGTH_SHORT).show();
                }
                break;
            //注册文本点击跳转
            case R.id.tv_regist:
                Intent intent2 = new Intent(this,RegistActivity.class);
                startActivity(intent2);
                break;
        }
    }

    private void login() {
        // 通过网络访问得到数据
        final String userName = etName.getText().toString();
        final String password = etPassword.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                String url = "http://192.168.43.221:8080/18/All/user?telephone="+etName.getText().toString();
                FormBody body = new FormBody.Builder().build();
                Request request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
                final Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    final String data = response.body().string();
                    Gson gson = new Gson();
                    final UserBean userBean = gson.fromJson(data,UserBean.class);
                    //
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //判断密码是否正确
                            if (data.length()==4||!etPassword.getText().toString().equals(userBean.getPassword())){
                                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }else {
                                Intent intent1 = new Intent(LoginActivity.this,FoodListActivity.class);
                                if (!etName.getText().toString().equals("")){
                                    // 通过SharedPreferences传输数据，供其他页面调用
                                    SharedPreferences sp = LoginActivity.this.getSharedPreferences("info",0);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("userName",etName.getText().toString());
                                    editor.putString("userPassword",etPassword.getText().toString());
                                    editor.commit();
                                }
                                startActivity(intent1);
                            }
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}